'''
Description: 爬取下厨房官网的图片
Version: 2.0
Autor: Wayne Liu
Date: 2021-05-07 16:35:57
LastEditors: Wayne Liu
LastEditTime: 2021-05-07 19:00:56
'''
import os
from urllib.parse import urlparse
from bs4 import BeautifulSoup
import requests

r = requests.get('https://www.xiachufang.com/')
# print(r)
soup = BeautifulSoup(r.text)

img_list = []
for img in soup.select('img'):
    if img.has_attr('data-src'):
        img_list.append(img.attrs['data-src'])
    else: 
        img_list.append(img.attrs['src'])

# 初始化下载目录环境
image_dir = os.path.join(os.curdir, 'images')
if not os.path.isdir(image_dir):
    os.mkdir(image_dir)

for img in img_list:
    o = urlparse(img)
    filename = o.path[1:]
    print(filename)
    filepath = os.path.join(image_dir, filename)
    url = '%s://%s%s' % (o.scheme, o.netloc, o.path)
    # print(url)
    resp = requests.get(url)
    with open(filepath, 'wb') as f:
        for chunk in resp.iter_content(1024):
            f.write(chunk)


# 遗留问题 - 以.svg结尾的会报错
# FileNotFoundError: [Errno 2] No such file or directory: '.\\images\\simpleicons/sinaweibo.svg'
